import cherrypy
import markup

class DbApp(object):
   """
   Interact with an existing Db via cherrypy and the www.
   """

   def __init__(self, db):
      """
      """
      self.db = db
      cherrypy.quickstart(self)

   def index(self):
       """
       """
       db = self.db
       yield '''
<html>
<head>
<style type="text/css">

#bodyid {
background-color:gray;
width:100%}

#navigation {
position: relative;
top: 10px;
xleft: 0;
width: 20%;
vertical-align: middle
}
		
#navigation a {
color: white;
text-decoration: none;
display: block;
background: blue;
padding: 0 0.5em;
margin: 0 1em 1px 1em;
}

#navigation a:hover {
background: green}

</style>
</head>
<body>'''
       yield '<h1>%s</h1>' % self.db
       yield '<div id="navigation">'
       for table in db.tables:
           yield '<a href="./pick_table?which=%s">%s</a>' % (table, table)
       yield '</div>'
       yield '''</body>
       </html>'''
   index.exposed = True

   def pick_table(self, which=None):
       """
       """
       db = self.db
       table = db[which]
       yield '<body bgcolor="lightblue">'
       yield '<h2> %s </h2>' % table
       yield '<font face="courier">'
       yield table.sql(table.get_rows_sql).pp().replace('\n', '<br/>').replace(' ', '&nbsp;')
       yield '</font>'
       yield '</body>'
   pick_table.exposed = True

   def tester(self):
       page= markup.page()
       page.init()
       print page
       return str(page)
   
   tester.exposed = True
